﻿using Inovout.Gis.Geometry;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace Inovout.Gis
{
    public abstract class GeometryLayerObject : LayerObject, IGeometryLayerObject, IGeometryLayerInfo
    {
        protected GeometryLayerObject()
        {
        }
        protected GeometryLayerObject(IGisTable gisTable)
            : base(gisTable)
        {
        }

        public GeometryLayerObject(IGeometryLayerInfo geometryLayerInfo)
            : base(geometryLayerInfo)
        {
        }
        private MapObject map;
        public MapObject Map
        {
            get { return map; }
            internal protected set { map = value; }
        }
        private List<IGeometryObject> geometries;
        public List<IGeometryObject> Geometries
        {
            get
            {
                if (geometries == null)
                {
                    geometries = new List<IGeometryObject>();
                    foreach (var geometryInfo in ((IGeometryLayerInfo)LayerInfo).Geometries)
                    {
                        GeometryObject geometryElement = (GeometryObject)geometryInfo;
                        geometryElement.Layer = this;
                        geometries.Add(geometryElement);
                    }
                }
                return geometries;
            }
        }


        IGeometryInfo[] IGeometryLayerInfo.Geometries
        {
            get { throw new System.NotImplementedException(); }
        }
        public IEnumerable Domains
        {
            get
            {
                ArrayList domains = new ArrayList();
                foreach (var polygonElement in Geometries)
                {
                    domains.Add(polygonElement.Domain);
                }
                return domains;
            }
        }

        public double AdjustmentArea
        {
            get { return CalculateAdjustmentArea(); }
        }

        protected virtual double CalculateAdjustmentArea()
        {
            return 0;
        }

        public double AvailableArea
        {
            get { return NetArea - AdjustmentArea; }
        }
        public double NetArea
        {
            get { return Geometries.Sum(geometry => geometry.NetArea); }
        }
        public double Area
        {
            get { return Geometries.Sum(geometry => geometry.Area); }
        }
    }
}
